#include "FreeRTOS.h"
#include "task.h"
#include "driver_function.h"
#include "lvgl.h"
#include "lv_port_disp_template.h"
#include "app_function.h"
#include "lv_port_indev_template.h"

#define SYSTEM_STATUS_STK_SIZE       32
#define SYSTEM_STATUS_TASK_PRIO      1   //if this value is high,this task Priority is high
TaskHandle_t System_Stauts_Task_Handler;


#define LVGL_STK_SIZE       1536
#define LVGL_TASK_PRIO      configMAX_PRIORITIES - 1   //if this value is high,this task Priority is high
TaskHandle_t LVGL_Task_Handler;

#define UART_TASK_PRIO     6
#define UART_STK_SIZE      512
TaskHandle_t UART_Task_Handler;


static void system_status_task(void *pvParameters);
static void lvgl_run_task(void *pvParameters);
static void uart_task_handler(void *pvParameters);


void app_freertos_init(void)
{
    xTaskCreate((TaskFunction_t ) system_status_task,
                (const char* )   "system_status_task",
                (uint16_t)        SYSTEM_STATUS_STK_SIZE,
                (void*)           NULL,
                (UBaseType_t)     SYSTEM_STATUS_TASK_PRIO,
                (TaskHandle_t*)  &System_Stauts_Task_Handler);

    xTaskCreate((TaskFunction_t ) lvgl_run_task,
                (const char* )   "lvgl_run_task",
                (uint16_t)        LVGL_STK_SIZE,
                (void*)           NULL,
                (UBaseType_t)     LVGL_TASK_PRIO,
                (TaskHandle_t*)  &LVGL_Task_Handler);


    xTaskCreate((TaskFunction_t ) uart_task_handler,
                    (const char* )   "uart_task_handler",
                    (uint16_t)        UART_STK_SIZE,
                    (void*)           NULL,
                    (UBaseType_t)     UART_TASK_PRIO,
                    (TaskHandle_t*)  &UART_Task_Handler);

    vTaskStartScheduler();
}

static void system_status_task(void *pvParameters)
{
    driver_led_init();
    while(1)
    {
        printf("system status task entry\r\n");
        driver_led_enable(DRIVER_LED_SYSTEM,DRIVER_LED_ENABLE);
        vTaskDelay(1000);
        driver_led_enable(DRIVER_LED_SYSTEM,DRIVER_LED_DISENABLE);
        vTaskDelay(1000);
    }
}

static void lvgl_run_task(void *pvParameters)
{
    lv_init();
    lv_port_disp_init();
    lv_fs_littlefs_init();
    app_littlefs_init();
    lv_port_indev_init();
    TickType_t last_wake = xTaskGetTickCount();
    const TickType_t period = pdMS_TO_TICKS(5);
    app_lvgl_main_ui();
    while(1)
   {
//       printf("lvgl run task entry\r\n");
       lv_task_handler();
       vTaskDelayUntil(&last_wake, period);
   }

}


static void uart_task_handler(void *pvParameters)
{
    driver_uart_obj uart_rx_date;
     char *send_test_cmd="AT+CWLAP\r\n";
    while(1)
        {
            printf("uart task entry\r\n");
            if (xQueueReceive(driver_uart_get_queue_handle(), &uart_rx_date, portMAX_DELAY) == pdPASS) { //have date
                if (strncmp((char*) uart_rx_date.rx_name,"usart2",sizeof(uart_rx_date.rx_name)) == 0) {
                    printf("uart2 Received %d bytes: ", uart_rx_date.rx_len);
                    for (uint16_t i = 0; i < uart_rx_date.rx_len; i++) {
                        printf("0x%02X ", uart_rx_date.rx_date[i]);
                    }
                    printf("\r\n");
                    if (uart_rx_date.rx_date[0] == 0xAA) {
//                        uart_hardware_uart4_send_date(send_test_cmd,strlen(send_test_cmd));
                        driver_wifi_send_cmd(send_test_cmd,strlen(send_test_cmd));
                        driver_camera_read_id();
                    }
                } else {
                    //wifi date
                    printf("uart4 Received %d bytes: \r\n", uart_rx_date.rx_len);
                    printf("recv=%s\r\n",uart_rx_date.rx_date);

                }

            }
        }
}



void vApplicationTickHook( void )
{
    lv_tick_inc(1);//for LVGL heart
}
